#include "widget.h"
#include "ui_widget.h"
#include "automation.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
//  , tray_(NULL)
{
    ui->setupUi(this);
    ui->stopButton->setVisible(false);

    connect(ui->startButton,SIGNAL(clicked(bool)),this,SLOT(onPlayerToggled(bool)));
    connect(ui->stopButton,SIGNAL(clicked(bool)),this,SLOT(onStopClicked(bool)));

    automate_ = new Automation();
    //automate_->initChain();

//    tray_ = new QSystemTrayIcon(this);
//    tray_->setIcon(QIcon(":/skin/heart.png"));
//    tray_->setToolTip("BetaZhu");
//    connect(tray_,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
//            this,SLOT(onSystemTrayClicked(QSystemTrayIcon::ActivationReason)));
//    connect(tray_,SIGNAL(messageClicked()),this,SLOT(onTrayMsgClicked()));

//    QMenu* menu = new QMenu(this);
    // add menu entry for binding dialog
//    QAction *confAct = menu->addAction(tr("Binding"));
//    connect(confAct, SIGNAL(triggered(bool)), this, SLOT(onBindingWnd()));
//    QAction *uploadAct = menu->addAction(tr("Upload"));
//    connect(uploadAct, SIGNAL(triggered(bool)), this, SLOT(onUploadWnd()));
//    QAction *quitAct = menu->addAction(tr("Exit"));
//    connect(quitAct, SIGNAL(triggered(bool)), this, SLOT(onActQuitApp()));
//    tray_->setContextMenu(menu);
//    tray_->show();
    connect(automate_,&Automation::sigHalt,this,&Widget::onHalt,Qt::QueuedConnection);
    connect(automate_,&Automation::sigHold,this,&Widget::onHold,Qt::QueuedConnection);
    connect(automate_,&Automation::sigFinished,this,&Widget::onFinished,Qt::QueuedConnection);
//    connect(ui->textEdit,&QTextEdit::textChanged,[=](){
//       qDebug()<<ui->textEdit->toPlainText();
//    });
}

Widget::~Widget()
{
    delete ui;
}

//void Widget::closeEvent(QCloseEvent *event)
//{
//    event->ignore();
//    hide();
//}

void Widget::generate(QStringList &accounts)
{
    accounts.clear();
    QString allText = ui->textEdit->toPlainText();
    int idx = allText.indexOf("\n");
    if(idx<0) return;
    allText = allText.mid(idx+1);
    QStringList org = allText.split("\n");
    for(int i=0;i+1<org.count();i+=2)
    {
        QString account = org.at(i);
        QString pasword = org.at(i+1);
        if(account.length()>12 || account.length()<=0) continue;
        if(pasword.length()>16 || pasword.length()<=0) continue;
        if(0!=account.indexOf(QRegExp("[0-9]+"))) continue;

        accounts << account << pasword;
    }
}

//void Widget::onSystemTrayClicked(QSystemTrayIcon::ActivationReason reason)
//{
//    qDebug("System tray icon clicked %d",reason);
//    if(reason == QSystemTrayIcon::Trigger)
//    {
//        if(this->isVisible() )
//        {
//            tray_->contextMenu()->popup(QCursor::pos());
//        }
//        else
//        {
//            show();
//        }
//    }
//}

//void Widget::onTrayMsgClicked()
//{
//    show();
//}

void Widget::onActQuitApp()
{

    QApplication::quit();
}

void Widget::onPlayerToggled(bool checked)
{
    playFlag_ = checked;

    if(playFlag_){
        QStringList accs;
        generate(accs);
        if(accs.count()==0)
        {
            qInfo()<<"no account";
            ui->statusLabel->setText("No accounts");
            ui->startButton->setChecked(false);
            return;
        }

        ui->stopButton->setVisible(true);
        ui->textEdit->setEnabled(false);
        ui->statusLabel->setText("Running...");
        automate_->setAccount(accs);
        automate_->doStart();
    }
    else{
        ui->stopButton->setVisible(true);
        ui->statusLabel->setText("Paused...");
        ui->textEdit->setEnabled(false);
        ui->startButton->setText("Resume");
        automate_->doPause();
    }
}

void Widget::onStopClicked(bool checked)
{
    automate_->doStop();
    ui->stopButton->setVisible(false);
    ui->statusLabel->setText("Stopped.");
    ui->textEdit->setEnabled(true);
    ui->startButton->setText("Start");
    ui->startButton->setChecked(false);
}

void Widget::onHalt(QString msg)
{
    ui->stopButton->setVisible(false);
    ui->startButton->setChecked(false);
    ui->statusLabel->setText("Halted.");
    ui->textEdit->setEnabled(true);
    ui->startButton->setText("Start");
}

void Widget::onHold(QString msg)
{
    ui->stopButton->setVisible(true);
    ui->startButton->setChecked(false);
    ui->statusLabel->setText("Hold.");
    ui->textEdit->setEnabled(false);
    ui->startButton->setText("Resume");
}

void Widget::onFinished()
{
    ui->stopButton->setVisible(false);
    ui->startButton->setChecked(false);
    ui->statusLabel->setText("Finished.");
    ui->textEdit->setEnabled(true);
    ui->startButton->setText("Start");
}
